﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoiffeurProject.DBMS.Entities;
using CoiffeurProject.DBMS.Utils;

namespace CoiffeurProject.DBMS.Tables
{
    public class Customers
    {
        Database database;

        List<Customer> customers;

        public Customers(String[] datas, Database database)
        {
            this.database = database;

            customers = new List<Customer>();

            for (int i = 0; i < datas.Length; i++)
            {
                var entries = datas[i].Split(new char[] { Constants.Separator });

                customers.Add(new Customer(int.Parse(entries[0]), entries[1], entries[2], entries[3], entries[4], entries[5],
                    entries[6], entries[7], DataConversion.StringToDate(entries[8])));
            }

            Customer.CURRENT_ID = customers.Count > 0 ? customers.Max(c => c.Id) : 0;
        }

        public void DeleteCustomer(Customer customer)
        {
            customers.Remove(customer);

            database.SaveTables();
        }

        public void Add(string name, string surname, string phone, string address, string sex, string email, string notes, DateTime birthday)
        { 
            customers.Add(new Customer(name, surname, phone, address, sex, email, notes, birthday));

            database.SaveTables();
        }

        internal void Update(int id, string n, string sr, string p, string a, string sx, string em, 
            string not, DateTime b)
        {
            var cust = customers.Find(c => c.Id == id);

            cust.Name = n;
            cust.Surname = sr;
            cust.Phone = p;
            cust.Address = a;
            cust.Sex = sx;
            cust.Email = em;
            cust.Notes = not;
            cust.Birthday = b;

            database.SaveTables();
        }

        public String[] ToStrings()
        {
            String[] s = new String[customers.Count];

            for (int i = 0; i < customers.Count; i++)
                s[i] = customers[i].ToString();

            return s;
        }

        public List<Customer> List
        {
            get { return customers; }
            set { customers = value; }
        }
    }
}
